package fr.xde.f2f.ws;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;

import com.google.common.collect.Maps;
import com.google.common.io.Files;

public class WSClientBean {
	Map<String,URI> endpoints = Maps.newHashMap();
	Map<String,File> templates = Maps.newTreeMap();
	FileSystemResource sampleDir = null;
	Map<String,String> samples = Maps.newHashMap();
	String template = null;
	String endpoint = null;
	String response = "nothing yet";
	WSClient wsc = new WSClient();
	
	public WSClientBean() throws URISyntaxException, IOException{
		endpoints.put("ws1", new URI("http://localhost:8180/face2faces/services/OrganizationAccess"));
		templates.put("request1", new File ("D:/home/workspace/faceTofaces/src/main/java/fr/xde/f2f/ws/request.xml"));
		sampleDir = new FileSystemResource("C:/temp/samples");
		for(File f : sampleDir.getFile().listFiles() )
		{
			samples.put(f.getName(), Files.toString(f, Charset.defaultCharset()));
		}

	}
	
	public Map<String, String> getSamples() {
		return samples;
	}

	public void setSamples(Map<String, String> samples) {
		this.samples = samples;
	}

	
	public void setEndpoints(Map<String, URI> endpoints) {
		this.endpoints = endpoints;
	}

	public void setTemplates(Map<String, File> templates) {
		this.templates = templates;
	}

	public Map<String, URI> getEndpoints() {
		return endpoints;
	}

	public Map<String, File> getTemplates() {
		return templates;
	}
	
	public FileSystemResource getSampleDir() {
		return sampleDir;
	}

	public void setSampleDir(FileSystemResource sampleDir) {
		this.sampleDir = sampleDir;
	}

	public String getTemplate() {
		return template;
	}

	public void setTemplate(String template) {
		this.template = template;
	}

	public String getEndpoint() {
		return endpoint;
	}

	public void setEndpoint(String endpoint) {
		this.endpoint = endpoint;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	public Map<String,String> listEndpoints() throws IOException{
		HashMap<String,String> ts = Maps.newHashMap();
		for(String k : endpoints.keySet()){
			ts.put(k, k);
		}
		return ts;
	}
	
	public void call() throws SOAPException, IOException{
		try {
			setResponse(wsc.call(endpoints.get(endpoint), template));
		} catch(Exception e) {
			setResponse(toString(e));
		}
	}
	
	public String toString(Exception e){
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);
		return sw.toString();
	}
	
}
